import 'package:common_utils/common_utils.dart';
import 'package:flutter/material.dart';
import 'package:xinxuan/common/appbar_util.dart';
import 'package:xinxuan/common/button_util.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:xinxuan/common/input_item.dart';
import 'package:xinxuan/common/scroll_configuration_common.dart';
import 'package:xinxuan/common/toggle_default_item.dart';
import 'package:xinxuan/controllers/my_controller/address_controller/address_controller.dart';
import 'package:xinxuan/router/app_routers.dart';
import 'package:x_common_utils/utils/inkWell_util.dart';
import 'package:xinxuan/utils/color_util.dart';

class AddEditAddressPage extends StatelessWidget {

  final AddressController addressController = Get.find();

  @override
  Widget build(BuildContext context) {
    Map<String, dynamic> argumentsMap = Get.arguments;
    String title = argumentsMap['title'];
    int type = argumentsMap['type'];
    if (argumentsMap['data'] != null) {
      Map<String, dynamic> mapData = argumentsMap['data'];
      addressController.ininEditAddress(mapData);
    } else {
      addressController.ininAddAddress();
    }
    return Scaffold(
      backgroundColor: pageBgColor,
      appBar: AppBarUtil(
        title: title,
        backgroundColor: pageBgColor,
        actions: [
          type == 0 ? InkWellUtil.shakeProofInkWell(Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Center(
              child: Text(
                '完成',
                style: TextStyle(
                  color: mainTextColor,
                  fontSize: 14.sp
                ),
              ),
            ),
          ), () {
            addressController.addAddress();
          },
          hoverColor: Colors.transparent,
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent) : InkWellUtil.shakeProofInkWell(Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Center(
              child: Text(
                '删除',
                style: TextStyle(
                  color: mainTextColor,
                  fontSize: 14.sp
                ),
              ),
            ),
          ), () {
            addressController.delAddress();
          },
          hoverColor: Colors.transparent,
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent)
        ],
      ),
      body: GetBuilder<AddressController>(
        builder: (controller) {
          return ScrollConfigurationCommon(child: ListView(
            children: [
              InputItem(
                title: '收货人', 
                hintText: '请填写收货人',
                defaultValue: '${addressController.consignee.value}', 
                onChenge: (String value) {
                  addressController.consignee.value = value;
                }
              ),
              InputItem(
                defaultValue: '${addressController.mobile.value}', 
                title: '联系电话', 
                hintText: '请填写联系电话', 
                keyBoardType: TextInputType.number,
                onChenge: (String value) {
                  addressController.mobile.value = value;
                }
              ),
              _cityLinkageItem(context),
              InputItem(
                defaultValue: '${addressController.address.value}', 
                title: '详细地址', 
                hintText: '请填写详细地址（街道、楼牌号等）', 
                onChenge: (String value) {
                  addressController.address.value = value;
                }
              ),
              ToggleDefaultItem(
                title: '设为默认地址', 
                value: addressController.isDefault.value == 1, 
                showBorder: false,
                onToggle: (bool value) {
                  addressController.isDefault.value = value ? 1 : 0;
                  addressController.update();
                }
              ),
              type == 0 ? Container() : Column(
                children: [
                  SizedBox(height: 54.h),
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 25.w),
                    child: ButtonUtil(
                      text: '保存', 
                      fontSize: 16.sp, 
                      borderRadius: 30.2, 
                      height: 36.h, 
                      onTop: () {
                        addressController.editAddress();
                      }
                    ),
                  )
                ],
              )
            ],
          ));
        },
      ),
    );
  }

  /// 三级联动
  Widget _cityLinkageItem(BuildContext context) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 12.w),
      color: bgWhiteColor,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 4.w),
        height: 54.h,
        decoration: BoxDecoration(
          border: Border(
            bottom: BorderSide(
              width: .5,
              color: lineColor
            )
          )
        ),
        child: Row(
          children: [
            Container(
              width: 84.w,
              child: Text(
                '所在地区',
                style: TextStyle(
                  color: textBlackColor,
                  fontSize: 16.sp
                ),
              ),
            ),
            Expanded(
              child: InkWellUtil.shakeProofInkWell(Container(
                child: Row(
                  children: [
                    Expanded(child: Text(
                      TextUtil.isEmpty(addressController.addressStr.value) ? '请选择' : addressController.addressStr.value,
                      style: TextStyle(
                        color: textBlackColor,
                        fontSize: 16.sp
                      ),
                    )),
                    SizedBox(width: 12.w),
                    Image.asset(
                      'assets/icons/icon_address_b.png',
                      width: 20.w,
                      height: 20.w
                    )
                  ],
                ),
              ), () async {
                Get.toNamed('${AppRouters.COMMON_PAGE}${AppRouters.MY_INFO_PAGE}${AppRouters.ADDRESS_LIST_PAGE}${AppRouters.ADD_EDIT_ADDRESS_PAGE}${AppRouters.ADDRESS_PROVINCE}');
              }),
            )
          ],
        ),
      ),
    );
  }

}